import animation
import geo2
import mathCommon
import miscUtil
import random
import aiming
import math
WALK_GAIT = 0.0
MAXIMUM_HEAD_LOOK_ANGLE = 2.5

class BipedAnimationController(animation.AnimationController):
    __guid__ = 'animation.BipedAnimationController'

    def Run(self):
        self.entityRef = None
        self.currentLOD = 0
        self.entityService = sm.GetService('entityClient')
        self.aimingClient = sm.GetService('aimingClient')



    def _UpdateHook(self):
        self._UpdateEntityInfo()
        self.SetCorrectLOD()
        self.UpdateMovement()
        self.UpdateIdlePose()
        self.UpdateLookAtTarget()



    def SetCorrectLOD(self):
        return 



    def InitIdleSpeed(self):
        idleSpeed = random.random()
        if (idleSpeed < 0.5):
            idleSpeed += 0.5
        self.SetControlParameter('IdlePlaybackSpeed', idleSpeed)



    def _UpdateEntityInfo(self):
        self.entPos = self.entityRef.movement.pos
        self.entRot = self.entityRef.movement.rot
        self.entVel = self.entityRef.movement.vel



    def UpdateLookAtTarget(self):
        if hasattr(self.entityRef, 'aiming'):
            gazeAtEntityID = self.aimingClient.GetTargetEntityID(self.entityRef, const.aiming.AIMING_VALID_TARGET_GAZE_ID)
            if gazeAtEntityID:
                gazeAtEntity = self.entityService.FindEntityByID(gazeAtEntityID)
                if gazeAtEntity:
                    eyePos = geo2.Vector(*self.entityRef.movement.pos)
                    eyePos.y = (eyePos.y + 1.62)
                    gazePos = geo2.Vector(*gazeAtEntity.position.position)
                    gazePos.y = (gazePos.y + 1.62)
                    eyeToGazeVector = geo2.Vec3Subtract(gazePos, eyePos)
                    absoluteGazeYaw = mathCommon.GetYawAngleFromDirectionVector(eyeToGazeVector)
                    absoluteGazeYaw = (absoluteGazeYaw + math.pi)
                    absoluteGazePitch = mathCommon.GetPitchAngleFromDirectionVector(eyeToGazeVector)
                    (entityYaw, entityPitch, trash,) = geo2.QuaternionRotationGetYawPitchRoll(self.entRot)
                    relativeLookAtYaw = (absoluteGazeYaw - entityYaw)
                    relativeLookAtPitch = (absoluteGazePitch - entityPitch)
                    relativeLookAtYaw = math.fmod(relativeLookAtYaw, (2 * math.pi))
                    relativeLookAtPitch = math.fmod(relativeLookAtPitch, (2 * math.pi))
                    if (relativeLookAtYaw > math.pi):
                        relativeLookAtYaw = (relativeLookAtYaw - (2 * math.pi))
                    if (relativeLookAtPitch > math.pi):
                        relativeLookAtPitch = (relativeLookAtPitch - (2 * math.pi))
                    if (abs(relativeLookAtYaw) < MAXIMUM_HEAD_LOOK_ANGLE):
                        self.SetControlParameter('Aim_X', -relativeLookAtYaw)
                    else:
                        self.SetControlParameter('Aim_X', 0.0)
                    if (abs(relativeLookAtPitch) < MAXIMUM_HEAD_LOOK_ANGLE):
                        self.SetControlParameter('Aim_Y', -relativeLookAtPitch)
                    else:
                        self.SetControlParameter('Aim_Y', 0.0)
                    return True
            return False



    def UpdateIdlePose(self):
        return 



    def SetEntityRef(self, ent):
        self.entityRef = ent
        self._UpdateEntityInfo()



    def UpdateMovement(self):
        debug = sm.GetService('debugRenderClient')
        yaw = geo2.QuaternionRotationGetYawPitchRoll(self.entityRef.movement.rot)[0]
        velYaw = mathCommon.GetYawAngleFromDirectionVector(self.entVel)
        angle = mathCommon.GetLesserAngleBetweenYaws(yaw, velYaw)
        newHeading = mathCommon.CreateDirectionVectorFromYawAngle(angle)
        newHeading = (-newHeading[0],
         newHeading[1],
         newHeading[2])
        actualSpeed = geo2.Vec3Length(self.entVel)
        localHeading = (newHeading if (actualSpeed > 0.01) else (0.0, 0.0, 0.0))
        localHeading = (round(localHeading[0]),
         round(localHeading[1]),
         round(localHeading[2]))
        moving = geo2.Vec3Length(localHeading)
        debug.RenderText(self.entityRef.movement.pos, (((('ActualSpeed: ' + str(actualSpeed)) + '\n') + 'Moving: ') + str(moving)), time=0, fade=False)
        self.SetControlParameter('Speed', round(moving))
        if moving:
            self.SetControlParameter('TurnAngle', 0.0)
        self.lastLocalHeading = localHeading




